//时间戳转日期
function date(timestampa) {
	var date = new Date(timestampa);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear();
	var M = (date.getMonth() + 1);
	M < 10 ? M = '0' + M : M;
	var D = date.getDate();
	D < 10 ? D = '0' + D : D;
	var h = date.getHours();
	h < 10 ? h = '0' + h : h;
	var m = date.getMinutes();
	m < 10 ? m = '0' + m : m;
	var s = date.getSeconds();
	s < 10 ? s = '0' + s : s;
	return Y +'-'+ M +'-'+ D// +' '+  h  +':'+  m +':'+ s//只要年月日
 }

export {   //关键
	date
}

//日期转时间戳年月日时分秒
function timetamp(time){
    var date = new Date();
    date.setFullYear(time.substring(0, 4));    //年
    date.setMonth(time.substring(5, 7) - 1);   //月
    date.setDate(time.substring(8, 10));	   //日
    date.setHours(time.substring(11, 13));	   //时
    date.setMinutes(time.substring(14, 16));   //分
    date.setSeconds(time.substring(17, 19));   //秒
    return Date.parse(date) / 1000;
}

export {   //关键
	timetamp
}

//日期转时间戳年月日
function timedata(time){
    var date = new Date();
    date.setFullYear(time.substring(0, 4));    //年
    date.setMonth(time.substring(5, 7) - 1);   //月
    date.setDate(time.substring(8, 10));	   //日
    // date.setHours(time.substring(11, 13));	   //时
    // date.setMinutes(time.substring(14, 16));   //分
    // date.setSeconds(time.substring(17, 19));   //秒
    return Date.parse(date) / 1000;
}

export {   //关键
	timedata
}

//过滤器
export function formatDate (date, format) {
    if (/(y+)/.test(format)) {
        format= format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    };
    for (let k in o) {
        if (new RegExp(`(${k})`).test(format)) {
            let str = o[k] + '';
            format= format.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
        }
    }
    return format;
};
 
function padLeftZero (str) {
    return ('00' + str).substr(str.length);
};

export function uploadImgToBase64 (file) {
    return new Promise((resolve, reject) => {
        const reader = new FileReader()
        reader.readAsDataURL(file)
        reader.onload = function () { // 图片转base64完成后返回reader对象
        resolve(reader)
        }
        reader.onerror = reject
    })
}